package com.yonyou.cms.epc.controller.shoppingCart;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.cms.epc.client.dto.shoppingCart.ShoppingCartDTO;
import com.yonyou.cms.epc.client.entity.shoppingCart.ShoppingCartPO;
import com.yonyou.cms.epc.client.feign.WmsServiceFeign;
import com.yonyou.cms.epc.service.shoppingCart.ShoppingCartService;
import com.yonyou.cyx.framework.controller.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @description: 购物车controller类
 * @author: chengbufang
 * @date: 2022-06-29 11:44
 */
@RestController
@RequestMapping("/graphic/shoppingCart")
public class ShoppingCartController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(ShoppingCartController.class);

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private WmsServiceFeign wmsServiceFeign;

    /**
     * 查询购物车列表
     * @param params
     * @return
     */
    @GetMapping("/getCartList")
    @ResponseBody
    public List<ShoppingCartPO> getCartList(@RequestParam Map<String, Object> params) {
        return shoppingCartService.getCartList(params);
    }

    /**
     * 购物车创建订单
     * @param paramMap
     * @return
     */
    @PostMapping("/reportOrder")
    @ResponseBody
    public Map<String, Object> reportOrder(@RequestBody Map<String, Object> paramMap) {
        log.info("=======================购物车创建订单开始=======================");
        Map<String, Object> result = new HashMap<>();

        // 校验备件是否符合订单类型
        log.info("=======================校验备件是否符合订单类型=======================");
        Integer count = shoppingCartService.checkOrderType(paramMap);
        if (count > 0) {
            log.info("=======================所选备件与订单类型不符=======================");
            log.info("=======================购物车创建订单完成=======================");
            result.put("resultCode", 103);
            result.put("errMsg", "所选备件与订单类型不符");

            return result;
        }

        List<ShoppingCartPO> cartList = (List) paramMap.get("orderParts");
        // 转换异常，需要先用JSON转一下
        String s = JSON.toJSONString(cartList);
        cartList = JSONObject.parseArray(s, ShoppingCartPO.class);

        log.info("=======================购物车订单开始调用feign接口提交=======================");
        List<Map<String, Object>> partList = shoppingCartService.reportOrder(cartList);
        log.info("=======================本次订单提交了：" + partList.size() + "个备件=======================");
        paramMap.put("orderParts", partList);
        result = (Map) wmsServiceFeign.epcImport(paramMap);
        log.info("=======================result：" + result.toString() + "=======================");
        log.info("=======================返回状态码：" + result.get("resultCode") + "=======================");
        if ((Boolean) result.get("success")) {
            log.info("=======================请求结果：" + result.get("success") + "=======================");
            // 成功删除购物车
            List<Long> deleteIds = cartList.stream().map(ShoppingCartPO::getId).collect(Collectors.toList());
            shoppingCartService.deleteFromCart(deleteIds);
        } else {
            log.info("=======================订单提交失败：" + result.get("errMsg") + "=======================");
        }
        log.info("=======================响应系统时间：" + result.get("elapsedMilliseconds") + "=======================");
        log.info("=======================购物车订单调用接口完成=======================");

        return result;
    }

    /**
     * 加入购物车
     * @param shoppingCartDto
     * @return
     */
    @PostMapping("/addToCart")
    @ResponseBody
    public Map<String, Object> pushToCart(@RequestBody ShoppingCartDTO shoppingCartDto) {
        return shoppingCartService.pushToCart(shoppingCartDto);
    }

    /**
     * 删除购物车
     * @param ids
     * @return
     */
    @DeleteMapping("/deleteFromCart")
    public Map<String, Object> deleteFromCart(@RequestBody List<Long> ids) {
        return shoppingCartService.deleteFromCart(ids);
    }

    /**
     * 查询购物车条数
     * @param params
     * @return
     */
    @GetMapping("/getCartCount")
    @ResponseBody
    public Integer getCartCount(@RequestParam Map<String, Object> params) {
        return shoppingCartService.getCartCount(params);
    }

    /**
     * 清空购物车
     * @param params
     * @return
     */
    @DeleteMapping("/clearCart")
    @ResponseBody
    public Map<String, Object> clearCart(@RequestParam Map<String, Object> params) {
        return shoppingCartService.clearCart(params);
    }
}
